ASP.NET Core in Action by Andrew Lock;
Author:Andrew Lock; [Lock, Andrew]
Language: eng
Format: epub
ISBN: 9781638356455
Publisher: Simon & Schuster
Published: 2021-04-13T05:00:00+00:00
Apply the filter to a controllerâAnonymous users could access actions from other controllers, but accessing any action on the protected controller would force them to log in.
Apply the filter globallyâUsers couldnât use the app without logging in. Any attempt to access an action or Razor Page would redirect the user to the login page.
Note ASP.NET Core comes with just such a filter out of the box: AuthorizeFilter. Iâll discuss this filter in section 13.2.1, and youâll be seeing a lot more of it in chapter 15.
As I described in the previous section, you normally create filters as attributes, and for good reasonâit makes it easy for you to apply them to MVC controllers, actions, and Razor Pages. In this section youâll see how to apply LogResourceFilter from listing 13.1 to an action, a controller, a Razor Page, and globally. The level at which the filter applies is called its scope.
Definition The scope of a filter refers to how many different actions it applies to. A filter can be scoped to the action method, to the controller, to a Razor Page, or globally.
Youâll start at the most specific scopeâapplying filters to a single action. The following listing shows an example of an MVC controller that has two action methods: one with LogResourceFilter and one without.
Listing 13.3 Applying filters to an action method
public class RecipeController : ControllerBase { [LogResourceFilter] â¶ public IActionResult Index() â¶ { â¶ return Ok(); â¶ } â¶ public IActionResult View() â· { â· return OK(); â· } â· }
â¶ LogResourceFilter will run as part of the pipeline when executing this action.
â· This action method has no filters at the action level.
Alternatively, if you want to apply the same filter to every action method, you could add the attribute at the controller scope, as in the next listing. Every action method in the controller will use LogResourceFilter, without having to specifically decorate each method.
Listing 13.4 Applying filters to a controller
[LogResourceFilter] â¶ public class RecipeController : ControllerBase { public IActionResult Index () â· { â· return Ok(); â· } â· public IActionResult View() â· { â· return Ok(); â· } â· }
â¶ The LogResourceFilter Is added to every action on the controller.
â· Every action in the controller is decorated with the filter.
For Razor Pages, you can apply attributes to your PageModel, as shown in the following listing. The filter applies to all page handlers in the Razor Pageâitâs not possible to apply filters to a single page handler; you must apply them at the page level.
Listing 13.5 Applying filters to a Razor Page
[LogResourceFilter] â¶ public class IndexModel : PageModel { public void OnGet() â· { â· } â· â· public void OnPost() â· { â· } â· }
â¶ The LogResourceFilter Is added to the Razor Pageâs PageModel.
â· The filter applies to every page handler in the page.
Filters you apply as attributes to controllers, actions, and Razor Pages are automatically discovered by the framework when your application starts up. For common attributes, you can go one step further and apply filters globally, without having to decorate individual classes.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7730)
Robo-Advisor with Python by Aki Ranin(7626)
Offensive Shellcode from Scratch by Rishalin Pillay(6106)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(5025)
Ego Is the Enemy by Ryan Holiday(4958)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4438)
Python for ArcGIS Pro by Silas Toms Bill Parker(4184)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3890)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3627)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3285)
Speed Up Your Python with Rust by Maxwell Flitton(3231)
Liar's Poker by Michael Lewis(3225)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3195)
Extreme DAX by Michiel Rozema & Henk Vlootman(3172)
Agile Security Operations by Hinne Hettema(3124)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3109)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3083)
Cryptography Algorithms by Massimo Bertaccini(3001)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2983)
